1
|
|
|
/** global: google */ |
2
|
|
|
|
3
|
|
|
import Marker from './Marker'; |
4
|
|
|
import {createRandomId} from '../functions/createRandomId'; |
5
|
|
|
|
6
|
|
|
class GoogleMap { |
7
|
|
|
|
8
|
|
|
constructor(container, options) { |
9
|
|
|
this.instanceId = options.instanceId || ''; |
10
|
|
|
this.container = container; |
11
|
|
|
this.markers = []; |
12
|
|
|
this.map = null; |
13
|
|
|
|
14
|
|
|
this.options = {}; |
15
|
|
|
this.defaults = { |
16
|
|
|
map: { |
17
|
|
|
center: { |
18
|
|
|
lat: 55.1309504, |
19
|
|
|
lng: 24.5499231 |
20
|
|
|
}, |
21
|
|
|
zoom: 7, |
22
|
|
|
scrollwheel: false, |
23
|
|
|
navigationControl: true, |
24
|
|
|
mapTypeControl: false, |
25
|
|
|
scaleControl: true, |
26
|
|
|
draggable: true, |
27
|
|
|
streetViewControl: false |
28
|
|
|
}, |
29
|
|
|
/** |
30
|
|
|
* show info window event type: |
31
|
|
|
* click - only show window when clicking on marker |
32
|
|
|
* hover - only show window when mouse over/out marker |
33
|
|
|
*/ |
34
|
|
|
infoWindowOn: 'click', |
35
|
|
|
globalMarkerIcon: '' |
36
|
|
|
}; |
37
|
|
|
|
38
|
|
|
// set options from data attribute |
39
|
|
|
this.placeInstanceId(options); |
40
|
|
|
this.populateOptions(options); |
41
|
|
|
this.initMap(); |
42
|
|
|
this.collectMarkers(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
placeInstanceId() { |
46
|
|
|
if (this.instanceId) { |
47
|
|
|
this.container.setAttribute('instance', this.instanceId); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
populateOptions(options = {}) { |
52
|
|
|
if('map' in options && 'styles' in options.map) { |
53
|
|
|
options.map.styles = JSON.parse(options.map.styles); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ('map' in options) { |
57
|
|
|
options.map = Object.assign(this.defaults.map, options.map); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
this.options = Object.assign(this.options, this.defaults, options); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
initMap() { |
64
|
|
|
this.map = new google.maps.Map(this.container, this.options.map); |
65
|
|
|
|
66
|
|
|
google.maps.event.addDomListener(window, 'resize', () => { |
67
|
|
|
this.map.setCenter(this.options.map.center); |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
collectMarkers() { |
72
|
|
|
let data = this.container.getAttribute('data-markers'); |
73
|
|
|
|
74
|
|
|
if (data != '' || typeof data != 'undefined') { |
75
|
|
|
data = JSON.parse(data); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
data.forEach((option) => { |
79
|
|
|
this.markers.push(new Marker(Object.assign(option, { |
80
|
|
|
instanceId: createRandomId(), |
81
|
|
|
infoWindowOn: this.options.infoWindowOn, |
82
|
|
|
icon: this.options.globalMarkerIcon != '' ? this.options.globalMarkerIcon : '', |
83
|
|
|
events: { |
84
|
|
|
onClick: (current) => this.markers.forEach((marker) => { |
85
|
|
|
if (current.getInstanceId() != marker.getInstanceId()) { |
86
|
|
|
marker.close(); |
87
|
|
|
} |
88
|
|
|
}) |
89
|
|
|
} |
90
|
|
|
}), this.map)); |
91
|
|
|
}); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
export default GoogleMap; |